package sky.dao.Impl;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import sky.dao.AirportDAO;
import sky.domain.Airport;
import sky.domain.City;
import sky.domain.Country;
import sky.tro.AirportFinder;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 23.04.13
 * Time: 10:28
 * To change this template use File | Settings | File Templates.
 */


@Repository
public class AirportDAOImpl implements AirportDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addAirport(Airport airport) {
        Query query = sessionFactory.getCurrentSession().createQuery("from City where name = ?");
        query.setParameter(0, airport.getCity().getName(), Hibernate.STRING);
        airport.setCity((City) query.uniqueResult());
        sessionFactory.getCurrentSession().save(airport);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Airport> listAirport() {
        return  sessionFactory.getCurrentSession().createQuery("from Airport").list();
    }

    @Override
    public void removeAirport(Integer id) {
        Airport airport = (Airport) sessionFactory.getCurrentSession().load(Airport.class, id);
        if (null != airport) {
            sessionFactory.getCurrentSession().delete(airport);
        }
    }

    @Override
    public Airport getAirportById(Integer id) {
        Airport airport = (Airport) sessionFactory.getCurrentSession().get(Airport.class, id);
        Hibernate.initialize(airport.getFlightsIn());
        Hibernate.initialize(airport.getFlightsOut());
        return airport;
    }

    @Override
    public void editAirport(Airport airport) {
        Query query = sessionFactory.getCurrentSession().createQuery("from City where name = ?");
        query.setParameter(0, airport.getCity().getName(), Hibernate.STRING);
        airport.setCity((City) query.uniqueResult());
        sessionFactory.getCurrentSession().update(airport);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<String> listCodes() {
        Query query = sessionFactory.getCurrentSession().createQuery("select code from Airport");
        return query.list();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Airport> listAirportByFinder(AirportFinder airportFinder) {
        StringBuilder queryString = new StringBuilder("from Airport as airport");
        if (!airportFinder.isEmpty()) {
            boolean notEmpty = false;
            queryString.append(" where");
            if (airportFinder.getCountryName()!=null) {
                queryString.append(" airport.city.country.name = ?");
                notEmpty = true;
            }
            if (airportFinder.getCityName()!=null) {
                if (notEmpty) queryString.append(" and");
                queryString.append(" airport.city.name = ?");
                notEmpty = true;
            }
            if (!airportFinder.getCode().equals("")) {
                if (notEmpty) queryString.append(" and");
                queryString.append(" airport.code = ?");
            }
        }
        Query query = sessionFactory.getCurrentSession().createQuery(queryString.toString());
        if (!airportFinder.isEmpty()) {
            int count = 0;
            if (airportFinder.getCountryName()!=null) {
                query.setParameter(count, airportFinder.getCountryName(), Hibernate.STRING);
                count++;
            }
            if (airportFinder.getCityName()!=null) {
                query.setParameter(count, airportFinder.getCityName(), Hibernate.STRING);
                count++;
            }
            if (!airportFinder.getCode().equals("")) {
                query.setParameter(count, airportFinder.getCode(), Hibernate.STRING);
            }
        }
        //System.out.println(query.getQueryString());
        return query.list();
    }

    @Override
    public Airport getAirportByName(String name) {
        Query query = sessionFactory.getCurrentSession().createQuery("from Airport where name = ? ");
        query.setParameter(0, name, Hibernate.STRING);
        Airport airport =  (Airport) query.uniqueResult();
        Hibernate.initialize(airport.getFlightsIn());
        Hibernate.initialize(airport.getFlightsOut());
        return airport;
    }
}
